package com.voicechat.firstmet.adapter;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.voicechat.firstmet.App;
import com.voicechat.firstmet.R;
import com.voicechat.firstmet.base.BaseAdapter;
import com.voicechat.firstmet.bean.PostBean;
import com.voicechat.firstmet.client.OssClient;
import com.voicechat.firstmet.client.OssConfig;
import com.voicechat.firstmet.utils.GlideLoadUtils;
import com.voicechat.firstmet.utils.Utils;
import com.voicechat.firstmet.view.CircleImageView;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @ProjectName: FirstMet
 * @Package: com.voicechat.firstmet.adapter
 * @ClassName: 社区listAdapter
 * @Description: java类作用描述
 * @Author: ljp
 * @CreateDate: 2018/9/4 14:11
 * @Version: 1.0
 */
public class DynamicPageAdapter extends BaseAdapter<PostBean.PageBean.ListBean> {
    private Context context;
    private List<PostBean.PageBean.ListBean> tlist;
    Thread thread;
    private String TAG = "DynamicPageAdapter";
    public ItemSetClick itemSetClick;
    private int position1;
    private String flag;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 1) {
                Bundle data = msg.getData();
                byte[] bitmap = data.getByteArray("bitmap");
                //String tag = data.getString("tag");
                CircleImageView view = (CircleImageView) msg.obj;
                GlideLoadUtils.getInstance().glideLoad(App.getContext(),bitmap,view);
                    /*GlideApp.with(context)
                            .load(bitmap)
                            .skipMemoryCache(true)
                            .diskCacheStrategy(DiskCacheStrategy.ALL)
                            .into(view);*/
            }
        }
    };

    public List<PostBean.PageBean.ListBean> getTlist() {
        return tlist;
    }

    /**
     * @param context activity_imessage
     * @param tlist   集合
     * @param mLayout 主布局
     */
    public DynamicPageAdapter(Context context, List<PostBean.PageBean.ListBean> tlist, int mLayout, String flag) {
        super(context, tlist, mLayout);
        this.tlist = tlist;
        this.context = context;
        this.flag = flag;

    }
    public void setItemSetClick(ItemSetClick itemSetClick) {
        this.itemSetClick = itemSetClick;
    }


    @Override
    protected RecyclerView.ViewHolder getHeaderViewHolder(View view) {
        return null;
    }

    @Override
    protected RecyclerView.ViewHolder getItemViewHolder(View view) {
        ViewHolder1 viewHolder1 = new ViewHolder1(view);
        return viewHolder1;
    }


    @Override
    protected RecyclerView.ViewHolder getFooterViewHolder(View view) {
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
        position1 = position;
         if (viewHolder instanceof ViewHolder1) { //普通布局
                ViewHolder1 viewHolder1 = (ViewHolder1) viewHolder;
                PostBean.PageBean.ListBean listBean = tlist.get(position);
                if(TextUtils.isEmpty(listBean.getUrl())){
                    viewHolder1.circlePicture.setVisibility(View.GONE);
                }
                //viewHolder1.imageIcon.setTag(listBean.getAvatarUrl());//设置标记
                //setImage(listBean.getAvatarUrl(), viewHolder1.imageIcon);//下载头像
             if (!listBean.getAvatarUrl().equals(viewHolder1.imageIcon.getTag(R.id.image_icon))) {
                 // 加载图片
                 viewHolder1.imageIcon.setTag(R.id.image_icon, listBean.getAvatarUrl());
                 GlideLoadUtils.getInstance().glideLoad(context,OssConfig.OSS_HTTP+listBean.getAvatarUrl(),
                         viewHolder1.imageIcon);
             }
                viewHolder1.tvTitle.setText(listBean.getMdTitle());
                if(!TextUtils.isEmpty(listBean.getVoiceUrl())){
                    viewHolder1.tvContent.setVisibility(View.GONE);
                    viewHolder1.linearSendVoice.setVisibility(View.VISIBLE);
                    viewHolder1.tvVoiceTime.setText(listBean.getDuration()+"s");
                }else {
                    viewHolder1.tvContent.setVisibility(View.VISIBLE);
                    viewHolder1.linearSendVoice.setVisibility(View.GONE);
                    viewHolder1.tvContent.setText(listBean.getContent());
                }
                viewHolder1.tvCommNumber.setText(listBean.getDiscussCount() + "");
                viewHolder1.tvLikeNumber.setText(listBean.getFabulousCount() + "");
                if(!TextUtils.isEmpty(listBean.getMdUpdateDate())) {
                    String standardDate = Utils.getStandardDate(listBean.getMdUpdateDate());//将返回时间转换为多久前
                    viewHolder1.tvDate.setText(standardDate);
                }
                if (!TextUtils.isEmpty(listBean.getuType()) && listBean.getuType().equals("2")) {//判断是否是主播
                    viewHolder1.imageIsanchor.setVisibility(View.VISIBLE);
                } else {
                    viewHolder1.imageIsanchor.setVisibility(View.GONE);
                }
                if(TextUtils.isEmpty(listBean.getUrl())){//判断是否有图片，如果没有图片，不显示图片
                    viewHolder1.circlePicture.setVisibility(View.GONE);
                }else {
                    viewHolder1.circlePicture.setVisibility(View.VISIBLE);
                }
             Log.i(TAG, "onBindViewHolder: -----"+listBean.toString());
                Log.i(TAG, "onBindViewHolder: ----position-----"+listBean.toString());
                if(listBean.getIsFabulous().equals("0")){//我是否点赞  1 已经点赞   0 未点赞
                    viewHolder1.imageLike.setImageResource(R.mipmap.unlike);
                }else {
                    viewHolder1.imageLike.setImageResource(R.mipmap.like);
                }
                if (!TextUtils.isEmpty(listBean.getvType()) &&
                        listBean.getvType().equals("1")) {//判断是vip等级 1 月 2 年 0 不是会员
                    viewHolder1.imageVip.setVisibility(View.VISIBLE);
                    viewHolder1.imageVip.setImageResource(R.mipmap.vip_month);
                } else if (listBean.getvType().equals("2")) {
                    viewHolder1.imageVip.setVisibility(View.VISIBLE);
                    viewHolder1.imageVip.setImageResource(R.mipmap.vip_year);
                } else {
                    viewHolder1.imageVip.setVisibility(View.GONE);
                }
                if (listBean.getGender() == 1){//判断性别  1男 2女
                    viewHolder1.imageSex.setImageResource(R.mipmap.boy);
                    viewHolder1.linearSexAge.setBackgroundResource(R.mipmap.sex_blue);
                }else if(listBean.getGender() == 2){
                    viewHolder1.imageSex.setImageResource(R.mipmap.girl);
                    viewHolder1.linearSexAge.setBackgroundResource(R.mipmap.sex_red);
                }
                if(!TextUtils.isEmpty(listBean.getBirthday())){
                    String substring = listBean.getBirthday().substring(0, 4);
                    String time = Utils.getStringTime(new Date());
                    int age = (Integer.parseInt(time)) - (Integer.parseInt(substring));
                    viewHolder1.tvAge.setText(age+"");
                }
                if(listBean.getStatus()== 0){//帖子审核状态，1审核通过 0审核中 2审核驳回
                    viewHolder1.tvDynamicState.setVisibility(View.VISIBLE);
                    viewHolder1.tvDynamicState.setText("审核中");
                }else if(listBean.getStatus()== 2){
                    viewHolder1.tvDynamicState.setVisibility(View.VISIBLE);
                    viewHolder1.tvDynamicState.setText("未通过");
                }else {
                    viewHolder1.tvDynamicState.setVisibility(View.GONE);
                }
                viewHolder1.tvNikeName.setText(listBean.getNickName());
                /*if(!TextUtils.isEmpty(listBean.getUrl())){
                    String[] strings = Utils.spitPictureUrl(listBean.getUrl());
                   // Log.i(TAG, "onBindViewHolder: "+strings.length);
                    setImage(strings[0],viewHolder1.circlePicture);
                }*/
             if(TextUtils.isEmpty(listBean.getUrl())){
                 viewHolder1.circlePicture.setVisibility(View.GONE);
             }else {
                 String[] strings = Utils.spitPictureUrl(listBean.getUrl());
                 if (!strings[0].equals(viewHolder1.circlePicture.getTag(R.id.circle_picture))) {
                     // 加载图片
                     viewHolder1.circlePicture.setTag(R.id.circle_picture, strings[0]);
                     GlideLoadUtils.getInstance().glideLoad(context,OssConfig.OSS_HTTP+strings[0],
                             viewHolder1.circlePicture);
                 }
             }
                viewHolder1.linearLike.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        itemSetClick.setLikeClick(v,listBean);
                    }
                });
                viewHolder1.imageIcon.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        itemSetClick.setIconClick(v, position1);
                    }
                });
                viewHolder1.linearDynamicBelow.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        itemSetClick.setBelowClick(v,listBean);
                    }
                });
             viewHolder1.itemView.setOnClickListener(new View.OnClickListener() {
                 @Override
                 public void onClick(View v) {
                     itemSetClick.setItemClick(viewHolder1.itemView, listBean);
                 }
             });
             viewHolder1.linearSendVoice.setOnClickListener(new View.OnClickListener() {
                 @Override
                 public void onClick(View v) {
                     itemSetClick.setPlayVoiceClick(listBean,viewHolder1.imageSendVoiceIcon,position);
                 }
             });

            }

    }

    private void setImage(String url, View view) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                OssClient.getInstance().init(context).fileDownload(url, new OssClient.OnDownloadFile() {
                    @Override
                    public void onDownFileSuccess(InputStream inputStream) {
                        byte[] bitmap = null;
                        try {
                            bitmap = OssConfig.readInputStreamToByte(inputStream);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        //if(view.getTag() != null && view.getTag().equals(url)) {
                            Message message = Message.obtain();
                            message.what = 1;
                            message.obj = view;
                            Bundle bundle = new Bundle();
                            //bundle.putString("tag",url);
                            bundle.putByteArray("bitmap", bitmap);
                            message.setData(bundle);
                            handler.sendMessage(message);
                      //  }
                    }

                    @Override
                    public void onDownFileProgress(String progress) {

                    }
                });
            }
        }).start();

    }
    /**
     * 布局1
     */
    class ViewHolder1 extends BaseViewHolder {
        @BindView(R.id.circle_picture)
        CircleImageView circlePicture;
        @BindView(R.id.tv_title)
        TextView tvTitle;
        @BindView(R.id.tv_content)
        TextView tvContent;
        @BindView(R.id.image_icon)
        CircleImageView imageIcon;
        @BindView(R.id.tv_nike_name)
        TextView tvNikeName;
        @BindView(R.id.tv_comm_number)
        TextView tvCommNumber;
        @BindView(R.id.tv_like_number)
        TextView tvLikeNumber;
        @BindView(R.id.tv_date)
        TextView tvDate;
        @BindView(R.id.image_isanchor)
        ImageView imageIsanchor;
        @BindView(R.id.image_vip)
        ImageView imageVip;
        @BindView(R.id.image_like)
        ImageView imageLike;
        @BindView(R.id.linear_like)
        LinearLayout linearLike;
        @BindView(R.id.image_sex)
        ImageView imageSex;
        @BindView(R.id.tv_age)
        TextView tvAge;
        @BindView(R.id.linear_sex_age)
        LinearLayout linearSexAge;
        @BindView(R.id.linear_dynamic_below)
        LinearLayout linearDynamicBelow;
        @BindView(R.id.tv_dynamic_state)
        TextView tvDynamicState;
        @BindView(R.id.image_send_voice_icon)
        ImageView imageSendVoiceIcon;
        @BindView(R.id.tv_voice_time)
        TextView tvVoiceTime;
        @BindView(R.id.linear_send_voice)
        RelativeLayout linearSendVoice;
        View itemView;
        public ViewHolder1(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            this.itemView = itemView;

        }
    }

    public interface ItemSetClick {
        void setIconClick(View view, int position);
        void setItemClick(View view, PostBean.PageBean.ListBean listBean);
        void setLikeClick(View view, PostBean.PageBean.ListBean listBean);
        void setBelowClick(View view,PostBean.PageBean.ListBean listBean);
        void setPlayVoiceClick(PostBean.PageBean.ListBean listBean, View view, int position);
    }
}
